package com.freeter.common.utils;


import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;


public class ZipUtil {

    /**
     *
     * @param srcSource 目录或文件
     * @return
     * @throws Exception
     */
    public static byte[] createZip(String srcSource) throws Exception{
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        //将目标文件打包成zip导出
        File file = new File(srcSource);
        doZip(zip,file,"");
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }



    public static void doZip(ZipOutputStream zip, File file, String dir) throws Exception {
        //如果当前的是文件夹，则进行进一步处理
        if (file.isDirectory()) {
            //得到文件列表信息
            File[] files = file.listFiles();
            //将文件夹添加到下一级打包目录
            zip.putNextEntry(new ZipEntry(dir + "/"));
            dir = dir.length() == 0 ? "" : dir + "/";
            //循环将文件夹中的文件打包
            for (int i = 0; i < files.length; i++) {
                doZip(zip, files[i], dir + files[i].getName());         //递归处理
            }
        } else {   //当前的是文件，打包处理
            //文件输入流
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));

            ZipEntry entry = new ZipEntry(dir);
            zip.putNextEntry(entry);
            zip.write(FileUtils.readFileToByteArray(file));
            IOUtils.closeQuietly(bis);
            zip.flush();
            zip.closeEntry();
        }
    }


    /**
     *
     * @param sourceOutput 二进制数组
     * @return
     * @throws Exception
     */
    public static byte[] createZip(ByteArrayOutputStream  sourceOutput,String fileName) throws Exception{
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        //将目标二进制打包成zip导出
        doZip(zip,sourceOutput,fileName);
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    /**
     *
     * @param sourceOutput 二进制数组
     * @return
     * @throws Exception
     */
    public static byte[] createZip(List<ByteArrayOutputStream>  sourceOutput,List<String> fileNames) throws Exception{
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        //将目标二进制打包成zip导出

        for(int i=0; i <sourceOutput.size();i++) {
            ByteArrayOutputStream byteout =sourceOutput.get(i);
            String fileName = fileNames.get(i);
            doZip(zip,byteout,fileName);
        }
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    public static void doZip(ZipOutputStream zip, ByteArrayOutputStream sourceOutput, String dir) throws Exception {
        //文件输入流
        ZipEntry entry = new ZipEntry(dir);
        zip.putNextEntry(entry);
        zip.write(sourceOutput.toByteArray());
        zip.flush();
        zip.closeEntry();
    }


}
